#include "TreeNode.h"
#include <math.h>

bool IsBalanced_Solution(TreeNode* pRoot) {
    if (pRoot == nullptr) {
        return true;
    }
    int left = depth(pRoot->left);
    int right = depth(pRoot->right);
    if (abs(left - right) > 1) {
        return false;
    }
    return IsBalanced_Solution(pRoot->left) && IsBalanced_Solution(pRoot->right);
}

int depth(TreeNode* pRoot) {
    if (pRoot == nullptr) {
        return 0;
    }
    int left = depth(pRoot->left) + 1;
    int right = depth(pRoot->right) + 1;
    return left < right ? right : left;
}